package com.lwm.stream;

import org.junit.Test;

import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.stream.LongStream;

public class ForkJoinTest {
    @Test
    public void test01() {
        Instant now = Instant.now();
        ForkJoinPool forkJoinPool = new ForkJoinPool();
        ForkJoinTask<Long> forkJoinCalculate = new ForkJoinCalculate(0, 1000000000L);
        Long sum = forkJoinPool.invoke(forkJoinCalculate);
        System.out.println(sum);
        Instant now1 = Instant.now();
        System.out.println("fork-join耗费时间为：" + Duration.between(now, now1).toMillis() + "毫秒！！！");


        Instant start = Instant.now();
        long su = 0;
        for (long i =0;i<=100000000000L;i++){
            su+=i;
        }
        System.out.println(su);
        Instant end = Instant.now();
        System.out.println("普通耗费时间为："+Duration.between(start, end).toMillis()+"毫秒！！！");
    }

    /**
     * Java并行流
     */
    @Test
    public void test02(){

        //225
        Instant start = Instant.now();
        LongStream
                .rangeClosed(0,1000000000L)
                .parallel()
                .reduce(0,Long::sum);
        Instant end = Instant.now();
        System.out.println("普通耗费时间为："+Duration.between(start, end).toMillis()+"毫秒！！！");
    }
}
